-- global_weaken
-- created by panyl
-- 对全体怪物附加伤害类状态

return {
    apply = function(source, target, skillId, round, para, extra)
        local effectRound = -1;
        extra = extra or {};
        local lastRound = extra.lastRound;

        if type(lastRound) == "number" then
            effectRound = round + lastRound;
        elseif para[5] > 0 then
            effectRound = round + para[5];
        end

        local prop = PropM.combine(source, "magic_last", skillId);
        effectRound = PropM.apply(prop, effectRound);
        prop = PropM.combine(source, "magic_last", "assist");
        effectRound = PropM.apply(prop, effectRound);

        -- 所有技能持续时间+
        prop = PropM.combine(source, "magic_last", "all");
        effectRound = PropM.apply(prop, effectRound);

        local targets = {};
        local grids = DungeonM.getCurrentDungeon();
        for _, grid in pairs(grids) do
            if  grid:isMonster() and
                grid:isOpened() and
                not grid.monster:isDead() then
                table.insert(targets, grid.monster);
            end
        end

        -- 魔力
        local magic = source:getMagic();
        -- 被对方百分比削弱
        prop = PropM.combine(target, "weak_enemy", "magic");
        magic = PropM.apply(prop, magic);

        -- 被对方绝对值削弱
        prop = PropM.combine(target, "weak_enemy2", "magic");
        magic = PropM.apply(prop, magic);

        -- 威力
        local force = para[4];
        -- 计算魔法效果加成
        local addon_rate = SkillM.calcMagicEffectAddon(source, skillId, true);
        -- 最终威力
        force = math.modf(force * (addon_rate + 1000)/1000);

        local v = math.modf(force * (magic + 3) / 1000);

        v = math.max(1, v);

        prop = { para[2], para[3], v, effectRound, }

        local condition = {
            ["prop"] = prop,
            ["end_round"] = effectRound,
        };

        for _, target in pairs(targets) do
            -- 增加命中动作
            --SkillM.getSequence():hit(source, target, skillId);

            -- 没被免疫
            if not SkillM.immunitySkill(source, target, skillId) then
                -- 附加状态
                CombatStatusM.applyStatus(target, para[1], condition);
            else
                if FormulaM.invoke("CAN_TARGET_INJURED", target, skillId) then
                    -- 如果不是无敌状态，则播放免疫的效果
                    SkillM.getSequence(target):immunity(source, target, skillId);
                end
            end
        end

        -- 返回作用目标
        return targets;
    end,

    parse = function(desc)
        local arr = string.explode(desc, ",");
        return { arr[1], tonumber(arr[2]), arr[3], tonumber(arr[4]), tonumber(arr[5] or -1), };
    end,

    -- 解析技能描述
    parseDesc = function(source, skillId, desc, para)
        -- 魔力
        local magic = source:getMagic();

        -- 威力
        local base = para[4];
        -- 计算魔法效果加成
        local addon_rate = SkillM.calcMagicEffectAddon(source, skillId, false);
        -- 最终威力
        local force = math.modf(base * (addon_rate + 1000)/1000);

        -- 效果/计算时魔力+3
        local vbase = math.modf(base * (magic + 3) / 1000);
        local vforce = math.modf(force * (magic + 3) / 1000);
        vbase = math.max(1, vbase);
        vforce = math.max(1, vforce);
        local vaddon = vforce - vbase;
        if vaddon == 0 then
            desc = string.gsub(desc, "{global_weaken}", vforce);
        else
            desc = string.gsub(desc, "{global_weaken}", vbase .. "(+" .. vaddon .. ")");
        end

        -- 回合数的显示
        local baseRound = para[5];
        local round = para[5];
        -- 增加持续时间的效果
        local prop = PropM.combine(source, "magic_last", skillId);
        round = PropM.apply(prop, round);
        prop = PropM.combine(source, "magic_last", "assist");
        round = PropM.apply(prop, round);
        prop = PropM.combine(source, "magic_last", "all");
        round = PropM.apply(prop, round);

        local addonRound = round - baseRound;
        if addonRound == 0 then
            desc = string.gsub(desc, "{round}", baseRound);
        else
            desc = string.gsub(desc, "{round}", baseRound .. "(+" .. addonRound .. ")");
        end

        return desc;
    end,
};
